<?php
  require_once("includes/common.php");
  $msg = isset($_SESSION['msg'])?$_SESSION['msg']:'';
  unset($_SESSION['msg']);


  if(!empty($_POST)) {

////////////// SET UNSET COOKIE ///////////////////
if(isset($_POST['remember'])){

  setcookie('email',$_POST['email'],time()+3600);//for 1 HOUR
  setcookie('password',$_POST['password'],time()+3600);//for 1 HOUR
} else {
  setcookie('email',$_POST['email'],time()-1);//for 1 HOUR
  setcookie('password',$_POST['password'],time()-1);//for 1 HOUR
}
//////////////////////////////////////////////////
    $_POST = array_map('trim',$_POST);
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
    $password = md5($password);

    $loginQuery = "SELECT * FROM `users` WHERE `email`='$email' AND `password`='$password' AND `status`='1'";
    $result = mysql_query($loginQuery);
    $userDetail = mysql_fetch_assoc($result);

    if(mysql_num_rows($result) == 1){

      $_SESSION['id'] =   $userDetail['id'];
      $_SESSION['email'] =   $userDetail['email'];
      $_SESSION['uname'] =   $userDetail['uname'];
      header('location:view_profile.php');
    } else {
      $msg = '<h4 style="color:red">Login Fail.Email/Passsword Incorrect</h4>';
    }

  }
  $cookieEmail = @$_COOKIE['email'];
  $cookiePassword = @$_COOKIE['password'];
?>

<center><?=$msg?></center>
<div style="float:left;">

<h3 style="text-align: center;">Login Here</h3>

<!--- Login message-->
<div></div>
   <link href="css/bootstrap.css" type="text/css" rel="stylesheet" />
   <link href="css/site.css" type="text/css" rel="stylesheet" />

<form class="form-horizontal" method="post">
  <div class="control-group">
    <label class="control-label" for="inputEmail">Email</label>
    <div class="controls">
      <input type="email" id="inputEmail" required placeholder="Email" name="email" value="<?=$cookieEmail?>">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="inputPassword">Password</label>
    <div class="controls">
      <input type="password" id="inputPassword" required placeholder="Password" name="password" value="<?=$cookiePassword?>">
    </div>
  </div>
  <div class="control-group">
    <div class="controls">
      <label class="checkbox">
        <input type="checkbox" name="remember" checked value="1"> Remember me
      </label>
      <button type="submit" class="btn btn-primary btn-large">Sign in</button>
      <a href="register.php">Register</a>
    </div>
  </div>
</form>
<div>
